import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//引入一级首页和框架
import Login from '@/views/login/index.vue'
import Layout from '@/layout/layout.vue'

//隐藏原地跳转错误
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

//路由一分为三

//1.静态路由：都能访问
export const constantRoutes = [
    //首页重定向
    {
        path: '/',
        redirect: '/home',
        hidden: true,
    },
    //登录
    {
        path: '/login',
        component: Login,
        hidden: true,
    },
    //首页
    {
        path: '/home',
        component: Layout,
        meta: { path: '/home', title: '后台首页', icon: 'home' },
        children: [{
            path: '',
            component: () =>
                import ('@/views/home/index.vue'),
        }, ],
    },
]

//2.动态路由：需要划分权限的路由
/* 
动态路由里面配置角色权限规则
1）如果所有人都能访问，不需要meta的roles字段 
2)如果设置访问权限meta:{roles:['可以访问的角色1'，'角色2']}
  父路由不能访问  子路由都不能访问
*/

export const asyncRoutes = [
    //订单管理
    {
        path: '/order',
        component: Layout,
        meta: { path: '/order', title: '订单管理', roles: ['super'], icon: 'order' },
        children: [{
                path: '',
                component: () =>
                    import ('@/views/order/index.vue'),
            },
            {
                hidden: true,
                path: '/order/order-look',
                component: () =>
                    import ('@/views/order/order-look.vue'),
            },

            {
                hidden: true,
                path: '/order/order-edit/:id',
                component: () =>
                    import ('@/views/order/order-edit.vue'),
            },
        ],
    },
    //商品管理
    {
        path: '/goods',
        component: Layout,
        redirect: '/goods/goods-list',
        meta: { path: '/goods', title: '商品管理', icon: 'goods' },
        children: [{
                path: '/goods/goods-list',
                meta: { path: '/goods/goods-list', title: '商品列表' },
                component: () =>
                    import ('@/views/goods/goods-list.vue'),
            },
            {
                path: '/goods/goods-add',
                meta: { path: '/goods/goods-add', title: '商品添加', roles: ['super'] },
                component: () =>
                    import ('@/views/goods/goods-add.vue'),
            },
            {
                path: '/goods/goods-cate',
                meta: { path: '/goods/goods-cate', title: '商品分类' },
                component: () =>
                    import ('@/views/goods/goods-cate.vue'),
            },
        ],
    },
    //店铺管理
    {
        path: '/shop',
        component: Layout,
        meta: { path: '/shop', title: '店铺管理', roles: ['super'], icon: 'shop' },
        children: [{
            path: '',
            component: () =>
                import ('@/views/shop/index.vue'),
        }, ],
    },
    //账号管理
    {
        path: '/account',
        component: Layout,
        redirect: '/account/account-list',
        meta: { path: '/account', title: '账号管理', icon: 'account' },
        children: [{
                path: '/account/account-list',
                meta: {
                    path: '/account/account-list',
                    title: '账号列表',
                    roles: ['super'],
                },
                component: () =>
                    import ('@/views/account/account-list.vue'),
            },
            {
                path: '/account/account-add',
                meta: {
                    path: '/account/account-add',
                    title: '账号添加',
                    roles: ['super'],
                },
                component: () =>
                    import ('@/views/account/account-add.vue'),
            },
            {
                path: '/account/password',
                meta: { path: '/account/password', title: '修改密码' },
                component: () =>
                    import ('@/views/account/password-modify.vue'),
            },
            {
                path: '/account/account-person',
                meta: { path: '/account/account-person', title: '个人中心' },
                component: () =>
                    import ('@/views/account/account-person.vue'),
            },
        ],
    },
    //销售统计
    {
        path: '/total',
        component: Layout,
        redirect: '/total/total-goods',
        meta: { path: '/total', title: '销售统计', roles: ['super'], icon: 'total' },
        children: [{
                path: '/total/total-goods',
                meta: { path: '/total/total-goods', title: '商品统计' },
                component: () =>
                    import ('@/views/total/total-goods.vue'),
            },
            {
                path: '/total/total-order',
                meta: { path: '/total/total-order', title: '订单统计' },
                component: () =>
                    import ('@/views/total/total-order.vue'),
            },
        ],
    },
]

//3.错误路由：404相关
export const errorRoutes = [
    /* 错误路由 */
    {
        path: '*',
        redirect: '/404',
    },
    {
        path: '/404',
        component: () =>
            import ('@/views/404/error404.vue'),
    },
]

const router = new VueRouter({
    //初始化加载静态路由
    routes: constantRoutes,
})
export default router